package DailySheet.Activity;

import hepengfei.SQL.SQLInterface;

import java.net.ContentHandler;
import java.util.ArrayList;
import java.util.HashMap;

import DailySheet.Activity.NewCourseAct.MyAdapter;
import DailySheet.Activity.ProfileChoice.newListener1;
import DailySheet.Activity.R;
import DailySheet.CourseManage.Course;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.text.style.SuperscriptSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CourseManageAct extends ListActivity{

	private DeleteAdapter deleteAdapter;
    private Button deleteBtn;
    private Boolean isDelete;
    ArrayList<HashMap<String, Object>> mapList;
    ArrayList<Boolean> isCheckedList;
    ArrayList<Course> courses;
    SQLInterface sqlInterface;
    HashMap<String, Object> map;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coursemanage);
	
		deleteBtn=(Button)findViewById(R.id.deleteBtn);
		deleteBtn.setVisibility(Button.INVISIBLE);
		
		isCheckedList=new ArrayList<Boolean>();
		mapList=new ArrayList<HashMap<String, Object>>();
		sqlInterface=new SQLInterface(this);
	    refreshCourses();
	    getListView().setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
			
				Intent intent=new Intent();
				intent.setClass(CourseManageAct.this, NewCourseAct.class);
				intent.putExtra("CourseId", courses.get(arg2).getCID());
				startActivity(intent);
				}
		});
	}

	public void refreshCourses(){
		courses=sqlInterface.FindAllCourse();
		mapList.clear();
		isCheckedList.clear();
		for(int i=0;i<courses.size();i++){
			map=new HashMap<String, Object>();
			map.put("courseName", courses.get(i).getCourseName());
			mapList.add(map);
		    isCheckedList.add(false);
		}
		isDelete=false;
		deleteBtn.setVisibility(Button.INVISIBLE);
		deleteAdapter=new DeleteAdapter(getBaseContext(), mapList);
		setListAdapter(deleteAdapter);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, 0, Menu.NONE, R.string.courseMenu_add);
		menu.add(Menu.NONE, 1, Menu.NONE, R.string.courseMenu_delete);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
        Intent intent=new Intent();
		switch(item.getItemId()){
		case 0:{
			intent.setClass(CourseManageAct.this, NewCourseAct.class);
			startActivity(intent);
			break;
		}
		case 1:{
			isDelete=true;
			deleteAdapter=new DeleteAdapter(this, mapList);
		    setListAdapter(deleteAdapter);
	        deleteBtn.setVisibility(Button.VISIBLE);
	        deleteBtn.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					for(int i=0;i<isCheckedList.size();i++){
						if(isCheckedList.get(i)){
							sqlInterface.DeleteCourseByCID(courses.get(i).getCID());
						}
					}
					refreshCourses();
				}
			});
		    break;
		}
		}
		
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
	}
    
	class DeleteAdapter extends BaseAdapter{
        private LayoutInflater inflater;
        private ArrayList<HashMap<String, Object>> array;
		public DeleteAdapter(Context context,ArrayList<HashMap<String, Object>> array){
			inflater=LayoutInflater.from(context);
			this.array=array;
		}
		public int getCount() {
			// TODO Auto-generated method stub
			return this.array.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View myView=inflater.inflate(R.layout.courselist1, null);
			
			TextView courseNameTextView=(TextView)myView.findViewById(R.id.course_Name1);
			CheckBox deleteCheBox=(CheckBox)myView.findViewById(R.id.deleteCheBox);
			courseNameTextView.setText((String) array.get(position).get("courseName"));
			final  int p=position;
			if(isDelete){
				deleteCheBox.setVisibility(CheckBox.VISIBLE);
			}
			else{
				deleteCheBox.setVisibility(CheckBox.INVISIBLE);
			}
			deleteCheBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
								if(isChecked){		
								    isCheckedList.set(p,true);
									Toast.makeText(getBaseContext(), (String)array.get(p).get("courseName")+isCheckedList.get(p)
											, Toast.LENGTH_SHORT).show();
								}
								else{
									isCheckedList.set(p, false);
									Toast.makeText(getBaseContext(), (String)array.get(p).get("courseName")+isCheckedList.get(p)
											, Toast.LENGTH_SHORT).show();
									
								}
				}
			});
			return myView;
		}
		
	}
	
}

